<?php

namespace Platform\Services\Video;

use Platform\Services\BaseService;

class VideoService extends BaseService {

    /**
     * 添加红包视频
     * @param type $userId
     * @param type $videoUrl
     * @param type $money
     * @param type $img
     * @param type $greeting
     */
    public function AddVideo($userId, $videoUrl, $money, $type, $img, $greeting) {
        $userId = intval($userId) ? $userId : E("用户ID不能为空");
        $videoUrl = trim($videoUrl) ? $videoUrl : E("视频地址不能为空");
        $money = doubleval($money);
        $type = intval($type);
        $greeting = trim($greeting);
        if (empty($img)) {
            $mp4Info = $this->getMp4cover($videoUrl);
            $img = $mp4Info['coverURL'];
        }
        $redPacketVideoModel = model('Video/RedPacketVideo');
        $redPacketVideoModel->setVideoUrl($videoUrl);
        $redPacketVideoModel->setUserId($userId);
        $redPacketVideoModel->setType($type);
        $redPacketVideoModel->setGetPacketCount(0);
        $redPacketVideoModel->setGetPacketMoney(0);
        $redPacketVideoModel->setImg($img);
        $redPacketVideoModel->setViewCount(0);
        $redPacketVideoModel->setGreeting($greeting);
        $redPacketVideoModel->setCreateTime(time());
        $redPacketVideoModel->setMoney($money);
        $redPacketVideoModel->setIsDel(IS_DEL_NO);
        $result = $redPacketVideoModel->save();
        if ($result == false) {
            $messages = $redPacketVideoModel->getMessages();
            foreach ($messages as $message) {
                E($message);
            }
        }
        $acodeUrl = $this->createRedPacketVideoAcode($redPacketVideoModel->getId());
        $redPacketVideoModel->setAcode($acodeUrl);
        $result = $redPacketVideoModel->save();
        if ($result == false) {
            $messages = $redPacketVideoModel->getMessages();
            foreach ($messages as $message) {
                E($message);
            }
        }
        return $redPacketVideoModel->toArray();
    }

    /**
     * 获取视频地址封面
     * @param type $videoUrl
     */
    public function getMp4cover($videoUrl) {
        if (strstr($videoUrl, OSS_RED_PACKET)) {
            $videoUrl = str_replace(OSS_RED_PACKET, "http://redpacket-video.oss-cn-hangzhou.aliyuncs.com", $videoUrl);
        }
        $videoInfo = lib('AliMTS')->getFileInfo($videoUrl);
        if (strstr($videoInfo['coverURL'], "http://redpacket-video-acode.oss-cn-hangzhou.aliyuncs.com")) {
            $videoInfo['coverURL'] = str_replace("http://redpacket-video-acode.oss-cn-hangzhou.aliyuncs.com", OSS_RED_PACKET_COVER, $videoInfo['coverURL']);
        } else {
            $videoInfo['coverURL'] = "https://ossredpacketimg.gjgj.com/pic_moren.png";
        }
        return $videoInfo;
    }

    /**
     * 生成红包视频小程序的二维码
     * @param type $id
     * @return string
     */
    public function createRedPacketVideoAcode($id) {
        $imgContentStream = service('Wechat/WechatService')->getMiniappCodeUnLimit("videoId=" . $id, "pages/reward/reward");
        $aliOssManage = new \Platform\Libs\AliOssManage("", "", \Platform\Libs\AliOssManage::HANGZHOU_HOSTNAME);
        $aliOssManage->putObject($aliOssManage::REDPACKET_ACODE_DIR . $id . ".png", $imgContentStream, $aliOssManage::REDPACKET_BUCKET, $options);
        $acodeUrl = OSS_RED_PACKET . "/" . $aliOssManage::REDPACKET_ACODE_DIR . $id . ".png";
        return $acodeUrl;
    }

    /**
     * 获取视频详情
     * @param type $id
     * @return type
     */
    public function getVideoInfoById($id) {
        $id = intval($id) ? intval($id) : E("视频id不能为空");
        $redPacketVideo = model('Video/RedPacketVideo');
        $videoInfoObject = $redPacketVideo->getDataById($id, $columns = '', $returnObject = true);
        $videoInfo = NULL;
        if ($videoInfoObject) {
            $videoInfo = $videoInfoObject->toArray();
        }
        if ($videoInfo['img'] == "https://ossredpacketimg.gjgj.com/pic_moren.png") {
            $mp4Info = $this->getMp4cover($videoInfo['videoUrl']);
            $videoInfoObject->setImg($mp4Info['coverURL']);
            $result == $videoInfoObject->save();
            if ($result == false) {
                $messages = $videoInfoObject->getMessages();
                foreach ($messages as $message) {
                    E($message);
                }
            }
        }
        return $videoInfo;
    }

    /**
     * 获取我的视频列表
     * @param type $userId
     * @param type $p
     * @param type $psize
     */
    public function getMyVideoList($userId, $p, $psize) {
        $p = intval($_POST['p']) ? intval($_POST['p']) : 1;
        $psize = intval($_POST['psize']) ? intval($_POST['psize']) : 10;
        $redPacketVideoModel = model('Video/RedPacketVideo');
        $condition['userId'] = $userId;
        $condition['isDel'] = IS_DEL_NO;
        $list = $redPacketVideoModel->getLists($condition, array(), "id desc", $p, $psize);
        return $list;
    }

    /**
     * 添加浏览日志记录
     * @param type $videoId
     * @param type $userId
     * @return boolean
     */
    public function addVideoViewLog($videoId, $userId) {
        $videoId = intval($videoId) ? intval($videoId) : E("视频id不能为空");
        $userId = intval($userId) ? intval($userId) : E("用户id不能为空");
        $videoViewLogModel = model("Video/VideoViewLog");
        $log = $videoViewLogModel->getDataByVideoIdAndUserId($videoId, $userId);
        if ($log) {
            return true;
        }
        $this->db->begin();
        $redPacketVideoModel = model("Video/RedPacketVideo");
        $redPacketVideoInfo = $redPacketVideoModel->getForUpdateDataById($videoId);
        if (!empty($redPacketVideoInfo)) {
            $redPacketVideoInfo->setViewCount($redPacketVideoInfo->getViewCount() + 1);
            $result = $redPacketVideoInfo->save();
            if ($result === FALSE) {
                $this->db->rollback();
                E("操作被打赏视频数据出错");
            }
        }
        $videoViewLogModel->setCreateTime(time());
        $videoViewLogModel->setUserId($videoId);
        $videoViewLogModel->setVideoId($userId);
        $result = $videoViewLogModel->save();
        if ($result === FALSE) {
            $this->db->rollback();
            E("添加视频日志出错");
        }
        $this->db->commit();
        return true;
    }

}

?>
